package com.ruihu.admin.util;

import cn.hutool.core.lang.Assert;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * 集合工具
 */
public class RhSetUtil {

    public static <T> Set<T> toSet(Iterable<T> iterable) {
        Assert.notNull(iterable);

        Set<T> set = new HashSet<>();
        Iterator<T> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            set.add(iterator.next());
        }

        return set;
    }

    public static <T> Set<T> duplicateElements(Iterable<T> iterable) {
        Assert.notNull(iterable);

        Set<T> set = new HashSet<>();
        Set<T> duplicateSet = new HashSet<>();
        Iterator<T> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            T t = iterator.next();
            if (set.contains(t)) {
                duplicateSet.add(t);
            } else {
                set.add(t);
            }
        }

        return duplicateSet;
    }
}
